package org.vison.wonfu.framework.config.utils;


import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.slf4j.Logger;
import org.springframework.core.env.Environment;

/**
 * @deprecated 即将废弃，直接使用 Spring Environment 来获取配置
 * 配置获取工具类，支持多级配置优先级：
 * 1. JVM参数(-D)
 * 2. Spring Environment
 * 3. Apollo配置中心
 */
@Deprecated
public class MultiLevelConfigProvider {

    private final static Logger logger = org.slf4j.LoggerFactory.getLogger(MultiLevelConfigProvider.class);

    private  Environment staticEnv;
    private  Config apolloConfig;


    private final String namespace;

    public MultiLevelConfigProvider(Environment environment, String apolloNamespace) {
        this.staticEnv = environment;
        this.namespace = apolloNamespace;
        initApolloConfig();
    }

    private void initApolloConfig() {
        try {
            // 获取指定命名空间的配置
            apolloConfig = ConfigService.getConfig(namespace);
        } catch (Exception e) {
            // 若未启用Apollo或配置错误，记录日志
            System.err.println("Apollo配置加载失败，命名空间: " + namespace + ", 错误: " + e.getMessage());
        }
    }

    /**
     * 获取配置值，按优先级：-D参数 > Apollo > Environment
     * @param key 配置键
     * @return 配置值，若不存在则返回null
     */
    public  String getString(String key) {
        // 1. 检查JVM参数(-D)
        String jvmValue = System.getProperty(key);
        if (jvmValue != null) {
            return jvmValue;
        }

        if (staticEnv != null && staticEnv.containsProperty(key)) {
           return staticEnv.getProperty(key);
        }
        if (apolloConfig != null) {
            String apolloValue = apolloConfig.getProperty(key, null);
            return apolloValue;
        }
        // 3. 检查Environment
        return null;
    }

    /**
     * 获取配置值，按优先级：-D参数 > Apollo > Environment，若不存在则返回默认值
     * @param key 配置键
     * @param defaultValue 默认值
     * @return 配置值或默认值
     */
    public  String getString(String key, String defaultValue) {
        String value = getString(key);
        return value != null ? value : defaultValue;
    }

    /**
     * 获取Integer类型配置值
     */
    public  Integer getInteger(String key) {
        String value = getString(key);
        return value != null ? Integer.valueOf(value) : null;
    }

    /**
     * 获取Integer类型配置值，若不存在则返回默认值
     */
    public  Integer getInteger(String key, Integer defaultValue) {
        Integer value = getInteger(key);
        return value != null ? value : defaultValue;
    }

    /**
     * 获取Boolean类型配置值
     */
    private   Boolean getBoolean(String key) {
        String value = getString(key);
        return value != null ? Boolean.valueOf(value) : null;
    }

    /**
     * 获取Boolean类型配置值，若不存在则返回默认值
     */
    public  Boolean getBoolean(String key, Boolean defaultValue) {
        Boolean value = getBoolean(key);
        return value != null ? value : defaultValue;
    }

    /**
     * 检查配置是否存在
     */
    public  boolean containsKey(String key) {
        return getString(key) != null;
    }
}